package com.vdong.restful.service.impl;

import com.vdong.data.model.Shipper;
import com.vdong.data.model.ShipperOrder;
import com.vdong.data.model.ShipperOrderUser;
import com.vdong.data.model.enums.OrderType;
import com.vdong.data.service.ShipperOrderService;
import com.vdong.data.service.ShipperOrderUserService;
import com.vdong.data.service.ShipperService;
import com.vdong.restful.api.request.ShipperOrderUserRequest;
import com.vdong.restful.api.response.Response;
import com.vdong.restful.service.ShipperApiService;
import com.vdong.restful.staticVariable.GlobalStaticVariable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShipperApiServiceImpl implements ShipperApiService {

    @Autowired
    private ShipperService shipperService;

    @Autowired
    private ShipperOrderUserService shipperOrderUserService;

    @Autowired
    private ShipperOrderService shipperOrderService;

    @Override
    public Response findByApplicationId(int applicationId) {

        List<Shipper> ls = shipperService.findByShipperApplicationId(applicationId);

        return Response.successList(ls);
    }

    @Override
    public Response addShipperOrderUser(ShipperOrderUserRequest shipperOrderUserRequest) {

        Shipper shipper = shipperService.findById(shipperOrderUserRequest.getShipperId());

        if (shipper.getPlaceOrderUsersType() == OrderType.ON) {
            ShipperOrder byShipperId = shipperOrderService.findByShipperId(shipperOrderUserRequest.getShipperId());
            if (byShipperId == null) {
                return Response.failMessage(GlobalStaticVariable.VALIDATION, "暂不支持该物流下单");
            }
            if (byShipperId.getCustomerName() == 0 && (shipperOrderUserRequest.getCustomerName() == null || shipperOrderUserRequest.getCustomerName().equals(""))) {
                return Response.failMessage(GlobalStaticVariable.VALIDATION, byShipperId.getCustomerNameDesc());
            } else if (byShipperId.getCustomerPwd() == 0 && (shipperOrderUserRequest.getCustomerPwd() == null || shipperOrderUserRequest.getCustomerPwd().equals(""))) {
                return Response.failMessage(GlobalStaticVariable.VALIDATION, byShipperId.getCustomerNameDesc());
            } else if (byShipperId.getMonthCode() == 0 && (shipperOrderUserRequest.getMonthCode() == null || shipperOrderUserRequest.getMonthCode().equals(""))) {
                return Response.failMessage(GlobalStaticVariable.VALIDATION, byShipperId.getMonthCodeDesc());
            } else if (byShipperId.getSendSite() == 0 && (shipperOrderUserRequest.getSendSite() == null || shipperOrderUserRequest.getSendSite().equals(""))) {
                return Response.failMessage(GlobalStaticVariable.VALIDATION, byShipperId.getSendSiteDesc());
            }

        }
        try {

            ShipperOrderUser shipperOrderUser = new ShipperOrderUser();
            shipperOrderUser.setCustomerName(shipperOrderUserRequest.getCustomerName());
            shipperOrderUser.setCustomerPwd(shipperOrderUserRequest.getCustomerPwd());
            shipperOrderUser.setMonthCode(shipperOrderUserRequest.getMonthCode());
            shipperOrderUser.setSendSite(shipperOrderUserRequest.getSendSite());
            shipperOrderUser.setShipperId(shipperOrderUserRequest.getShipperId());
            shipperOrderUser.setCreateTime(LocalDateTime.now());
            shipperOrderUser.setUsersKey(shipperOrderUserRequest.getUsersKey());
            shipperOrderUser.setUserId(shipperOrderUserRequest.getUserId());
            shipperOrderUserService.save(shipperOrderUser);
            return Response.successBean("success");
        } catch (org.springframework.dao.DataIntegrityViolationException e) {
            e.printStackTrace();
            return Response.failMessage(GlobalStaticVariable.EXCEPTION, "不要重复添加");
        }
    }

    @Override
    public Response saveShipperOrderUser(ShipperOrderUserRequest shipperOrderUserRequest) {
        Shipper shipper = shipperService.findById(shipperOrderUserRequest.getShipperId());

        if (shipper.getPlaceOrderUsersType() == OrderType.ON) {
            ShipperOrder byShipperId = shipperOrderService.findByShipperId(shipperOrderUserRequest.getShipperId());
            if (byShipperId == null) {
                return Response.failMessage(GlobalStaticVariable.VALIDATION, "暂不支持该物流下单");
            }
            if (byShipperId.getCustomerName() == 0 && (shipperOrderUserRequest.getCustomerName() == null || shipperOrderUserRequest.getCustomerName().equals(""))) {
                return Response.failMessage(GlobalStaticVariable.VALIDATION, byShipperId.getCustomerNameDesc());
            } else if (byShipperId.getCustomerPwd() == 0 && (shipperOrderUserRequest.getCustomerPwd() == null || shipperOrderUserRequest.getCustomerPwd().equals(""))) {
                return Response.failMessage(GlobalStaticVariable.VALIDATION, byShipperId.getCustomerNameDesc());
            } else if (byShipperId.getMonthCode() == 0 && (shipperOrderUserRequest.getMonthCode() == null || shipperOrderUserRequest.getMonthCode().equals(""))) {
                return Response.failMessage(GlobalStaticVariable.VALIDATION, byShipperId.getMonthCodeDesc());
            } else if (byShipperId.getSendSite() == 0 && (shipperOrderUserRequest.getSendSite() == null || shipperOrderUserRequest.getSendSite().equals(""))) {
                return Response.failMessage(GlobalStaticVariable.VALIDATION, byShipperId.getSendSiteDesc());
            }

        }
        ShipperOrderUser shipperOrderUser = shipperOrderUserService.get(shipperOrderUserRequest.getId());
        if (shipperOrderUser != null) {
            shipperOrderUser.setCustomerName(shipperOrderUserRequest.getCustomerName());
            shipperOrderUser.setCustomerPwd(shipperOrderUserRequest.getCustomerPwd());
            shipperOrderUser.setMonthCode(shipperOrderUserRequest.getMonthCode());
            shipperOrderUser.setSendSite(shipperOrderUserRequest.getSendSite());
            shipperOrderUser.setShipperId(shipperOrderUserRequest.getShipperId());
            shipperOrderUser.setUpdateTime(LocalDateTime.now());


            shipperOrderUserService.save(shipperOrderUser);
            return Response.successBean("success");
        } else {
            return Response.failMessage(GlobalStaticVariable.NO, "没有匹配的信息");
        }


    }

    @Override
    public Response findOneOrderUser(String userId, String key, int shipperId) {
        ShipperOrderUser shipperOrderUser = shipperOrderUserService.findbyShipperIdAndUserIdAndKey(shipperId, userId, key);
        if (shipperOrderUser != null) {
            return Response.successBean(shipperOrderUser);
        } else {
            return Response.failMessage(GlobalStaticVariable.NO, "没有匹配的信息");
        }


    }

    @Override
    public Response findListOrderUser(String userId, String key) {
        List<ShipperOrderUser> byUserIdAndKey = shipperOrderUserService.findByUserIdAndKey(userId, key);
        if (byUserIdAndKey != null) {
            return Response.successBean(byUserIdAndKey);
        } else return Response.failMessage(GlobalStaticVariable.NO, "没有匹配的信息");
    }

    @Override
    public Response findOneOrderValidataon(int shipperId) {

        ShipperOrder byShipperId = shipperOrderService.findByShipperId(shipperId);
        if (byShipperId != null) {
            return Response.successBean(byShipperId);
        } else
            return Response.failMessage(GlobalStaticVariable.NO, "没有匹配的信息");
    }




}
